package com.test.moredatasourse.utils;

import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

public class RedissonBloomFilter {

    public static void main(String[] args) {
        Config config = new Config();
        //单redis实例方式
//        config.useSingleServer().setAddress("redis://192.168.197.24:36379");

        //指定使用哨兵部署方式
        config.useSentinelServers()
                //设置sentinel.conf配置里的sentinel别名
                //比如sentinel.conf里配置为sentinel monitor mymaster 192.168.197.24 6379 2,那么这里就配置mymaster
                .setMasterName("mymaster")
                //这里设置sentinel节点的服务IP和端口，sentinel是采用Paxos拜占庭协议，一般sentinel至少3个节点
                //记住这里不是配置redis节点的服务端口和IP，sentinel会自己把请求转发给后面monitor的redis节点
                .addSentinelAddress("redis://192.168.197.24:26379")
                .addSentinelAddress("redis://192.168.197.24:26380")
                .addSentinelAddress("redis://192.168.197.24:26381");


        RedissonClient redisson = Redisson.create(config);

        RBloomFilter<String> bloomFilter = redisson.getBloomFilter("user");
        // 初始化布隆过滤器，预计统计元素数量为55000000，期望误差率为0.03
        bloomFilter.tryInit(55000000L, 0.03);
        bloomFilter.add("yaokj");
        bloomFilter.add("jamaly");
        System.out.println(bloomFilter.count());   //2
        System.out.println(bloomFilter.contains("yaokj"));  //true
        System.out.println(bloomFilter.contains("xiaokua"));  //false

        //最后关闭RedissonClient
        redisson.shutdown();
    }

}
